﻿namespace Innovative.ProXr
{
	/// <summary>
	/// Provides a full set of static methods to communicate with the relay controller
	/// supporting all documented commands.
	/// </summary>
	public static partial class DirectApi
	{
		/// <summary>
		/// Under normal operation, you will send a command to the relay controller, and the relay controller will respond to your 
		/// command by activating or deactivating a relay. This system works well if you only need to control 1-8 relays, but it 
		/// does not necessarily work very well if you are taking advantage of the XR Expansion port, allowing you to control up 
		/// to 2048 relays (255 relay banks depending on controller). In these cases, you may want to set the status of all 
		/// relays at the ex-act same time. The easiest way to do this is to turn off automatic relay refreshing. Once turned 
		/// off, you can use the relay control command set to activate relays, but the commands will not appear to have any effect. 
		/// The effects will not be seen until you manually refresh the relay bank. Rest assured, when auto refreshing is off, 
		/// your relay control commands are working, the processor memory is copied to the physical relay bank memory when you 
		/// manually refresh the relays. Follow this methodology to set the status of lots of relays at one time across multiple banks:
		/// a) Turn Off Auto Refreshing.
		/// b) Use Relay Control Command to activate different relays on different banks.
		/// These commands will not appear to work; they will only modify internal memory.
		/// c) Send the Manual Refresh Command to update all relays at one time.
		/// </summary>
		public static class AutomaticRelayRefreshing
		{
			/// <summary>
			/// Turn ON Automatic Relay Refreshing. When a relay control command is sent to the controller, the controller 
			/// will refresh the state of all relays in every bank. This command has been modified from the original version, 
			/// it no longer stores the refresh status in EEPROM. Use Device Configuration to change how the controller 
			/// functions on startup.
			/// </summary>
			public static RelayCommandSet<bool> TurnOn = new RelayCommandSet<bool>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 25)
				},
				ExpectedResults = new RelayByte[] { 85 },
				ResponseConverter = new Boolean85ResponseConverter()
			};

			/// <summary>
			/// Turn OFF Automatic Relay Refreshing. When a relay control command is sent, the relay will not change 
			/// state. This allows you to use the complete ProXR command set to manipulate relay status. When you need 
			/// the relays to change state, you will send a manual refresh command. This command no longer stores the 
			/// refresh status in EEPROM. Use Device Configuration to change how the controller functions on startup.
			/// </summary>
			public static RelayCommandSet<bool> TurnOff = new RelayCommandSet<bool>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 26)
				},
				ExpectedResults = new RelayByte[] { 85 },
				ResponseConverter = new Boolean85ResponseConverter()
			};

			/// <summary>
			/// This command reports the current status of automatic relay refreshing. This command will return a 0 or 1 
			/// indicating if refreshing is off or on.
			/// </summary>
			public static RelayCommandSet<RelayStatus> GetStatus = new RelayCommandSet<RelayStatus>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 36)
				},
				ExpectedResults = new RelayByte[] { 0, 1 },
				ResponseConverter = new RelayStatusResponseConverter()
			};

			/// <summary>
			/// Manually refresh all relays in all relay banks. Use this command to set the status of all relays at the exact 
			/// same time. Use in combination with Relay Refreshing commands
			/// </summary>
			public static RelayCommandSet<bool> RefreshAllRelays = new RelayCommandSet<bool>()
			{
				Commands = new RelayCommand[]
				{
					RelayCommand.Command254,
					RelayCommand.CreateReadOnlyCommand(1, 37)
				},
				ExpectedResults = new RelayByte[] { 85 },
				ResponseConverter = new Boolean85ResponseConverter()
			};
		}
	}
}
